package com.cutthroatcreations.woodenrailway;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SignUpActivity extends Activity {

	private static final String TAG = "SignUpActivityLog";

	private EditText email, pw, pwCon;
	private Button signUp;
	private Context ctx;
	private ProgressDialog progressDialog;
	private User user;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.ctx = this;
		setContentView(R.layout.signup);
		email = (EditText) findViewById(R.id.SUEmail);
		pw = (EditText) findViewById(R.id.SUPassword);
		pwCon = (EditText) findViewById(R.id.SUconfirmPassword);
		signUp = (Button) findViewById(R.id.SUSignUpBtn);

		signUp.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
				User u = new User(email.getText().toString(), pw.getText()
						.toString(), pwCon.getText().toString());
				user = u;
				if (isValidEmail(user.getEmail())) {
					postParameters.add(new BasicNameValuePair("email", user
							.getEmail()));

					if (user.pwMatch()) {
						postParameters.add(new BasicNameValuePair("password",
								user.getPw()));
						postParameters.add(new BasicNameValuePair("app", "1"));
						postParameters.add(new BasicNameValuePair("url",
								"users/add"));

						runRequest(postParameters);
					} else {
						Toast.makeText(ctx, R.string.supassword,
								Toast.LENGTH_SHORT).show();
					}
				} else {
					Toast.makeText(ctx, R.string.emailinval, Toast.LENGTH_SHORT)
							.show();
				}

			}
		});
	}

	private final static boolean isValidEmail(CharSequence target) {
		if (target == null) {
			return false;
		} else {
			return Patterns.EMAIL_ADDRESS.matcher(target).matches();
		}
	}

	private void runRequest(ArrayList<NameValuePair> postParameters) {

		AsyncTask<ArrayList<NameValuePair>, Void, String> run = new request();
		if (!postParameters.isEmpty()) {
			run.execute(postParameters);
		} else {
			Toast.makeText(ctx, R.string.server, Toast.LENGTH_SHORT).show();
		}
	}

	public class request extends
			AsyncTask<ArrayList<NameValuePair>, Void, String> {

		@Override
		protected void onPreExecute() {
			progressDialog = ProgressDialog.show(ctx, "CONTACTING SERVER",
					"Please Wait....", true);
		};

		@Override
		protected String doInBackground(ArrayList<NameValuePair>... params) {

			String res = null;
			String response = null;
			try {
				response = CustomHttpClient.executeHttpPost(params[0].get(3)
						.getValue().toString(), params[0]);

				res = response.toString();
				res = res.replaceAll("\\s+", "");
				res = res.trim();
				Log.i(TAG, res);
				return res;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}

		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);

			Intent intent = new Intent(getApplication(),
					WoodenrailwayActivity.class);

			if (result == null) {
				Toast.makeText(ctx, R.string.server, Toast.LENGTH_SHORT).show();
			} else if (result.equals("1")) {
				Toast.makeText(ctx, R.string.success, Toast.LENGTH_SHORT)
						.show();
				progressDialog.dismiss();
				intent.putExtra("email", user.getEmail());
				startActivity(intent);
			} else if (result.equals("2")) {
				Toast.makeText(ctx, R.string.suemail, Toast.LENGTH_SHORT)
						.show();
			} else {
				Toast.makeText(ctx, R.string.problem, Toast.LENGTH_SHORT)
						.show();
			}
			progressDialog.dismiss();

		}

	}

}
